package com.gpframework.module.ctn.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gpframework.common.utils.StringUtils;
import com.gpframework.module.ctn.domain.GpCtnOrder;
import com.gpframework.module.ctn.domain.GpCtnReachPort;
import com.gpframework.module.ctn.domain.req.GpCtnReachPortUpdate;
import com.gpframework.module.ctn.domain.vo.GpCtnReachPortExcel;
import com.gpframework.module.ctn.domain.vo.GpCtnReachPortVo;
import com.gpframework.module.ctn.mapper.GpCtnReachPortMapper;
import com.gpframework.module.ctn.service.IGpCtnReachPortService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
/**
 * @ClassName GpCtnReachPort
 * @Author gpframework
 * @Date 2024-01-08
 * @Version 1.0.0
 * @Description 装柜到港填写Service业务层处理
 */
@Service
public class GpCtnReachPortServiceImpl extends ServiceImpl<GpCtnReachPortMapper, GpCtnReachPort> implements IGpCtnReachPortService {
    @Autowired
    private GpCtnLoadedServiceImpl gpCtnLoadedService;
    @Autowired
    private GpCtnOrderServiceImpl gpCtnOrderService;

    /**
     * @Description 查询列表返回VO 用于返回给前端的列表接口
     * @Author gpframework
     * @Date 2024-01-08
     * @Param [lqw]
     * @Return List<GpCtnReachPortVo>
     */
    @Override
    public List<GpCtnReachPortVo> listVo(QueryWrapper<GpCtnReachPort> lqw) {
        return this.baseMapper.listVo(lqw);
    }

    /**
     * @Description 通过查询详情VO 用于返回给前端的列详情接口
     * @Author gpframework
     * @Date 2024-01-08
     * @Param [id]
     * @Return GpCtnReachPortVo
     */
    @Override
    public GpCtnReachPortVo getVoById(Long id) {
        return this.baseMapper.getVo(id);
    }

    @Override
    public List<GpCtnReachPortExcel> gpCtnReachPortImport(List<GpCtnReachPortExcel> excelList, Long brandId, Long storeId) {
        //导入失败的列表
        List<GpCtnReachPortExcel> failureList = new ArrayList<>();
        //装柜到港填写
        GpCtnReachPort gpCtnReachPort;

        for (int i = 0; i < excelList.size(); i++) {
            GpCtnReachPortExcel excel = excelList.get(i);
            if (StringUtils.isNull(excel)) {
                excel = new GpCtnReachPortExcel();
                excel.setSubscript(i+1);
                excel.setMsg("读取数据失败");
                failureList.add(excel);
                continue;
            }
            gpCtnReachPort = Convert.convert(new TypeReference<GpCtnReachPort>() {}, excel);
            this.save(gpCtnReachPort);
        }
        return failureList;
    }


    @Override
    public boolean editCtnReachPort(GpCtnReachPortUpdate gpCtnReachPortUpdate) {
        GpCtnReachPort gpCtnReachPort = this.getOne(new QueryWrapper<GpCtnReachPort>().eq("ctn_order_id", gpCtnReachPortUpdate.getId()));
        //修改货柜到港信息
        if (gpCtnReachPort!=null){
            GpCtnReachPort ctnReachPort = Convert.convert(new TypeReference<GpCtnReachPort>() {}, gpCtnReachPort);
            ctnReachPort.setFreightPrice(gpCtnReachPortUpdate.getFreightPrice());
            ctnReachPort.setReachDestinationDate(gpCtnReachPortUpdate.getReachDestinationDate());
            this.updateById(ctnReachPort);
        }
        //修改订货柜信息
        if (gpCtnReachPort.getCtnOrderId()!=null){
            GpCtnOrder ctnOrder = Convert.convert(new TypeReference<GpCtnOrder>() {}, gpCtnReachPortUpdate);
            gpCtnOrderService.updateById(ctnOrder);
        }


        return true;
    }
}
